﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;




namespace KZ400B.Controls
{
    public class AxlePositionLable : ContentControl
    {
        protected Storyboard _flash;
        public AxlePositionLable()
        {
            this.DefaultStyleKey = typeof(AxlePositionLable);
            this.Loaded += new RoutedEventHandler(AxlePositionLable_Loaded);
        }

        public static DependencyProperty AxleNameProperty = DependencyProperty.Register("AxleName", typeof(string), typeof(AxlePositionLable), new PropertyMetadata(string.Empty));
        public string AxleName
        {
            get { return (string)GetValue(AxleNameProperty); }
            set { SetValue(AxleNameProperty, value); }
        }
        public Color TfColor
        {
            get { return (Color)GetValue(TfColorProperty); }
            set { SetValue(TfColorProperty, value); }
        }
        public static DependencyProperty TfColorProperty = DependencyProperty.Register("TfColor", typeof(Color), typeof(AxlePositionLable), new PropertyMetadata(Colors.DarkGray));

        public Color AbnormColor
        {
            get { return (Color)GetValue(AbnormColorProperty); }
            set { SetValue(AbnormColorProperty, value); }
        }
        public static DependencyProperty AbnormColorProperty = DependencyProperty.Register("AbnormColor", typeof(Color), typeof(AxlePositionLable), new PropertyMetadata(Colors.Red));

        public Color NormalColor
        {
            get { return (Color)GetValue(NormalColorProperty); }
            set { SetValue(NormalColorProperty, value); }
        }
        public static DependencyProperty NormalColorProperty = DependencyProperty.Register("NormalColor", typeof(Color), typeof(AxlePositionLable), new PropertyMetadata(Color.FromArgb(255, 128, 255, 128)));

        public Color TextColor
        {
            get { return (Color)GetValue(TextColorProperty); }
            set { SetValue(TextColorProperty, value); }
        }

        public static DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(Color), typeof(AxlePositionLable), new PropertyMetadata(Color.FromArgb(255, 0, 255, 0), new PropertyChangedCallback(OnTextColorInvalidated)));

        private static void OnTextColorInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.Foreground = new SolidColorBrush(control.TextColor);
        }

        public Color BackgroundColor
        {
            get { return (Color)GetValue(BackgroundColorProperty); }
            set { SetValue(BackgroundColorProperty, value); }
        }

        public static DependencyProperty BackgroundColorProperty = DependencyProperty.Register("BackgroundColor", typeof(Color), typeof(AxlePositionLable), new PropertyMetadata(Colors.Transparent, new PropertyChangedCallback(OnBackgroundColorInvalidated)));//

        private static void OnBackgroundColorInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.Background = new SolidColorBrush(control.BackgroundColor);
        }

        public short Tf
        {
            get { return (short)GetValue(TfProperty); }
            set { SetValue(TfProperty, value); }
        }

        public static DependencyProperty TfProperty = DependencyProperty.Register("Tf", typeof(short), typeof(AxlePositionLable), new PropertyMetadata((short)0, new PropertyChangedCallback(OnTfInvalidated)));

        private static void OnTfInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.OnChanged();
        }

        public short Abnorm
        {
            get { return (short)GetValue(AbnormProperty); }
            set { SetValue(AbnormProperty, value); }
        }

        public static DependencyProperty AbnormProperty = DependencyProperty.Register("Abnorm", typeof(short), typeof(AxlePositionLable), new PropertyMetadata((short)0, new PropertyChangedCallback(OnAbnormInvalidated)));

        private static void OnAbnormInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.OnChanged();
        }


        public static DependencyProperty UnitProperty = DependencyProperty.Register(
                                "Unit", typeof(string), typeof(AxlePositionLable)
                                , new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnUnitInvalidated)));
        public string Unit
        {
            get { return (string)GetValue(UnitProperty); }
            set { SetValue(UnitProperty, value); }
        }

        private static void OnUnitInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.OnChanged();
        }

        public override void OnApplyTemplate()
        {
            //base.OnApplyTemplate();
            //_point = ModelCacheManager.Instance[typeof(SC.Point), Pid] as SC.Point;
            ////_mesg = ModelCacheManager.Instance[typeof(SC.Mesg), Pid] as SC.Mesg;//有问题，pid
            //OnBindPoint(_point); 
        }

        void AxlePositionLable_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                return;
            //_point = ModelCacheManager.Instance[typeof(SC.Point), Pid] as SC.Point;
            //if (_point != null)
            //{
            //    ScadaClientProxy.Instance.Registe(typeof(SC.Point), Pid);
                  OnBindAxle(null); 
            //}
            //else
            //    ScadaClientProxy.Instance.LoadModelCompleted += new EventHandler(Instance_LoadModelCompleted);
        }

        void Instance_LoadModelCompleted(object sender, EventArgs e)
        {
            //_point = ModelCacheManager.Instance[typeof(SC.Point), Pid] as SC.Point;
            //if (_point != null)
            //{
            //    ScadaClientProxy.Instance.Registe(typeof(SC.Point), Pid);
            //    OnBindPoint(_point);
            //}
        }
        private Axle m_Axle;
        public virtual void OnBindAxle(Axle axle)
        {
            //m_Point = point;
            //Border border = new Border()
            //{
            //    Background = new SolidColorBrush(Colors.White),
            //    Margin = new Thickness(-30, -2, -17, -2)
            //};
            //border.Child = new SCLabelToolTip() { Point = point };
            ContextMenu menu = new ContextMenu();
            //menu.Items.Add(border);
            MenuItem realSeriesItem = new MenuItem() { Header = "实时曲线" };
            MenuItem sethiloInfoItem = new MenuItem() { Header = "测点属性设置" };
            MenuItem accidentInfoItem = new MenuItem() { Header = "查看报警信息" };
            MenuItem dealAccidentInfoItem = new MenuItem() { Header = "处理报警信息" };
            MenuItem managAccidentPointItem = new MenuItem() { Header = "报警点信息管理" };
            MenuItem cancelAlarmItem = new MenuItem() { Header = "报警确认" };
            //realSeriesItem.Click += new RoutedEventHandler(realSeriesItem_Click);
            //sethiloInfoItem.Click += new RoutedEventHandler(sethiloInfoItem_Click);
            //accidentInfoItem.Click += new RoutedEventHandler(accidentInfoItem_Click);
            //dealAccidentInfoItem.Click += new RoutedEventHandler(dealAccidentInfoItem_Click);
            //managAccidentPointItem.Click += new RoutedEventHandler(managAccidentPointItem_Click);
            //cancelAlarmItem.Click += new RoutedEventHandler(cancelAlarmItem_Click);
            menu.Items.Add(realSeriesItem);
            //if (m_Point is SC.Ana)
            //    menu.Items.Add(sethiloInfoItem);
            menu.Items.Add(accidentInfoItem);
            menu.Items.Add(dealAccidentInfoItem);
            menu.Items.Add(managAccidentPointItem);
            menu.Items.Add(cancelAlarmItem);
            ContextMenuService.SetContextMenu(this, menu);
            //ToolTipService.SetToolTip(this, new SCLabelToolTip() { Point = point });
        }

        //void sethiloInfoItem_Click(object sender, RoutedEventArgs e)
        //{
        //    SuperControl.Controls.Dialog.PointPropertySetterDialog dialog = 
        //        new SuperControl.Controls.Dialog.PointPropertySetterDialog(m_Point as SC.Ana);
        //    dialog.Show();
        //}

        //void managAccidentPointItem_Click(object sender, RoutedEventArgs e)
        //{
        //    AccidentPointManagDialog frmPoint = new AccidentPointManagDialog();
        //    frmPoint.PointPid = m_Point.Pid;
        //    frmPoint.IsPopup = true;
        //    frmPoint.Show();
        //}

        //void dealAccidentInfoItem_Click(object sender, RoutedEventArgs e)
        //{
        //    if (m_Point != null)
        //    {
        //        AccidentClient.Dialog.DealAccidentInfoDialog dialog = new AccidentClient.Dialog.DealAccidentInfoDialog();
        //        dialog.AccidentPointRid = m_Point.Rid;
        //        Alarm alarm = ModelCacheManager.GetList<Alarm>().FirstOrDefault(p => p.Point == m_Point.Rid);
        //        Mesg mesg = ModelCacheManager.GetList<Mesg>().FirstOrDefault(p => p.Rid == alarm.Mesg);
        //        if (mesg != null)
        //        {
        //            dialog.AccidentMesgRid = mesg.Rid;
        //        }
        //        else
        //        {
        //            dialog.AccidentMesgRid = 0;
        //        }
        //        dialog.IsPopu = true;
        //        dialog.Show();
        //    }
        //    else
        //    {
        //        MessageBox.Show("没有采集到此点的数据！");
        //    }
        //}

        //void cancelAlarmItem_Click(object sender, RoutedEventArgs e)
        //{
        //    if (m_Point == null)
        //    {
        //        return;
        //    }
        //    var Alarms = m_Point.ChildrenAlarm;
        //    foreach (var alarm in Alarms)
        //    {
        //        if (alarm.Ack == 0)
        //        {
        //            Alarm cloneAlarm = (Alarm)alarm.Clone();
        //            cloneAlarm.Ack = 1;
        //            ScadaClientProxy.Instance.Excute(ExcuteType.Update, cloneAlarm);
        //        }
        //    }

        //}

        //void accidentInfoItem_Click(object sender, RoutedEventArgs e)
        //{
        //    if (m_Point != null)
        //    {
        //        AccidentClient.Dialog.ViewAccidentInfoDialog dialog = new AccidentClient.Dialog.ViewAccidentInfoDialog();
        //        dialog.SelectedPointRid = m_Point.Rid;
        //        Alarm alarm = ModelCacheManager.GetList<Alarm>().FirstOrDefault(p => p.Point == m_Point.Rid);
        //        Mesg mesg=ModelCacheManager.GetList<Mesg>().FirstOrDefault(p => p.Rid == alarm.Mesg);
        //        if (mesg != null)
        //        {
        //            dialog.SelectedMesgRid = mesg.Rid;
        //        }
        //        else
        //        {
        //            dialog.SelectedMesgRid = 0;
        //        }              
        //        dialog.Show();
        //    }
        //    else
        //    {
        //        MessageBox.Show("此点数据采集为空！");
        //    }
        //}

        //void realSeriesItem_Click(object sender, RoutedEventArgs e)
        //{
        //    Border border = new Border()
        //    {
        //        Background = new SolidColorBrush(Colors.White),
        //        Margin = new Thickness(5)
        //    };
        //    border.Child = new SCLabelToolTip() { Point = m_Point };
        //    ChildWindow window = new ChildWindow() ;
        //    window.Content = border;
        //    window.Show();
        //}

        private void OnChanged()
        {
            BackgroundColor = Colors.Transparent;
            if (Tf != 0)
            {
                BackgroundColor = TfColor;
            }
            if (Abnorm != 0)
            {
                TextColor = AbnormColor;
            }
        }

        public bool IsInfoLeft
        {
            get { return (bool)GetValue(IsInfoLeftProperty); }
            set { SetValue(IsInfoLeftProperty, value); }
        }
        public static DependencyProperty IsInfoLeftProperty = DependencyProperty.Register("IsInfoLeft", typeof(bool), typeof(AxlePositionLable), new PropertyMetadata(true));

        public virtual string Prefix
        {
            get { return (string)GetValue(PrefixProperty); }
            set { SetValue(PrefixProperty, value); }
        }

        public static DependencyProperty PrefixProperty = DependencyProperty.Register("Prefix", typeof(string), typeof(AxlePositionLable), new PropertyMetadata(string.Empty));

        public Brush PrefixBrush
        {
            get { return (Brush)GetValue(PrefixBrushProperty); }
            set { SetValue(PrefixBrushProperty, value); }
        }
        public static DependencyProperty PrefixBrushProperty = DependencyProperty.Register("PrefixBrush", typeof(Brush), typeof(AxlePositionLable), new PropertyMetadata(new SolidColorBrush(Colors.White)));

        //public Brush NormalColor
        //{
        //    get { return (Brush)GetValue(NormalColorProperty); }
        //    set { SetValue(NormalColorProperty, value); }
        //}
        //public static DependencyProperty NormalColorProperty = DependencyProperty.Register("NormalColor", typeof(Color), typeof(SCLabelBase), new PropertyMetadata(Color.FromArgb(255, 128, 255, 128)));

        public bool IsFlash
        {
            get { return (bool)GetValue(IsFlashProperty); }
            set { SetValue(IsFlashProperty, value); }
        }

        public static DependencyProperty IsFlashProperty = DependencyProperty.Register("IsFlash", typeof(bool), typeof(AxlePositionLable), new PropertyMetadata(false, new PropertyChangedCallback(OnIsFlashInvalidated)));

        private static void OnIsFlashInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.OnFlash();
        }

        public TimeSpan FlashInterval
        {
            get { return (TimeSpan)GetValue(FlashIntervalProperty); }
            set { SetValue(FlashIntervalProperty, value); }
        }

        public static DependencyProperty FlashIntervalProperty = DependencyProperty.Register("FlashInterval", typeof(TimeSpan), typeof(AxlePositionLable), new PropertyMetadata(TimeSpan.FromSeconds(0.5), new PropertyChangedCallback(OnFlashIntervalInvalidated)));

        private static void OnFlashIntervalInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AxlePositionLable control = (AxlePositionLable)d;
            control.OnFlash();
        }
        protected virtual void OnFlash()
        {
            if (_flash != null)
                _flash.Stop();
            if (IsFlash)
            {
                _flash = new Storyboard();
                Storyboard.SetTarget(_flash, this);
                _flash.RepeatBehavior = RepeatBehavior.Forever;
                ColorAnimationUsingKeyFrames colorAnimation = new ColorAnimationUsingKeyFrames();
                colorAnimation.KeyFrames.Add(
                    new LinearColorKeyFrame()
                    {
                        Value = TextColor,
                        KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.0))
                    });
                colorAnimation.KeyFrames.Add(
                    new LinearColorKeyFrame()
                    {
                        Value = AbnormColor,
                        KeyTime = KeyTime.FromTimeSpan(FlashInterval)
                    });
                colorAnimation.KeyFrames.Add(
                    new LinearColorKeyFrame()
                    {
                        Value = TextColor,
                        KeyTime = KeyTime.FromTimeSpan(FlashInterval + FlashInterval)
                    });
                Storyboard.SetTargetProperty(colorAnimation, new PropertyPath(AxlePositionLable.TextColorProperty));
                _flash.Children.Add(colorAnimation);
                _flash.Begin();
            }
        }
    }
}
